package dolf.objects;

import java.awt.*;
import dolf.game.*;
import dolf.objects.WorldObject;

public class Animation extends WorldObject implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean loop = false;
	private int currImageIndex = 0;
	private java.util.LinkedList<String> imgVec;
	private Point pos;
	private int z;
	
	
	public Animation(java.util.LinkedList<String> vec) {
		super(false);
		this.imgVec = vec;
		
		this.z = 0;
		
		//cache image in SpriteStore to avoid slow first animation
		for (int i = 0; i < imgVec.size(); i++) {
			ImageStore.getInstance().getImage(imgVec.get(i));
		}
		
	}
	public void startAt(Point p, boolean l) {
		this.loop = l;
		this.pos = p;
		Level.getInstance().registerAnimation(this);
	}
	public void stop() {
		Level.getInstance().removeAnimation(this);
	}
	
	public void paint(Graphics2D g) {
		if(z == 1){ //same again, animation slower
			g.drawImage(ImageStore.getInstance().getImage(imgVec.get(currImageIndex)), pos.getIntX() , pos.getIntY(), null);
			z = ++z % 2;
		} else if (currImageIndex < imgVec.size()) {
			g.drawImage(ImageStore.getInstance().getImage(imgVec.get(currImageIndex)), pos.getIntX() , pos.getIntY(), null);
			if (this.loop) currImageIndex = ((currImageIndex + 1) % imgVec.size());
			else currImageIndex++;
		} else {//finished, remove from level
			this.stop();
		}
	}
	
	public void setLoop(boolean l) {
		this.loop = l;
	}
}
